package com.zsl.controller.Admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zsl.model.Book;
import com.zsl.service.BookService;
import com.zsl.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "GetBookManageTable",urlPatterns = "/get/book/manage/table")
public class GetBookManageTable extends HttpServlet {
    BookService bookService=new BookServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Map<String, Object> res = new HashMap<>();

        //获取所有的书籍信息
        try {
            res=bookService.getAllBook();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            res.put("total",0);
            res.put("rows",new JSONArray());
        }finally {
            PrintWriter out = response.getWriter();
            out.write(JSON.toJSONString(res));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
